package com.ctshk.rpc.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.system.dto.MainDataPaymentRefundTypeDTO;
import com.ctshk.rpc.system.entity.MainDataPaymentRefundType;
import com.ctshk.rpc.system.mapper.MainDataPaymentRefundTypeMapper;
import com.ctshk.rpc.system.req.MainDataPaymentRefundTypeIdReq;
import com.ctshk.rpc.system.req.MainDataPaymentRefundTypeListReq;
import com.ctshk.rpc.system.req.MainDataPaymentRefundTypeReq;
import com.ctshk.rpc.system.service.IMainDataPaymentRefundTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.dubbo.config.annotation.DubboService;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 收款/退款方式表 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-03-16
 */
@DubboService
public class MainDataPaymentRefundTypeServiceImpl extends ServiceImpl<MainDataPaymentRefundTypeMapper, MainDataPaymentRefundType> implements IMainDataPaymentRefundTypeService {

    @Override
    public Result add(MainDataPaymentRefundTypeReq req, TokenUser tokenUser) {
        LocalDateTime now = LocalDateTime.now();
        long id = IdWorker.getId();
        MainDataPaymentRefundType paymentRefundType = EntityUtil.copy(req, MainDataPaymentRefundType.class);
        paymentRefundType.setId(id);
        paymentRefundType.setCreateId(tokenUser.getId());
        paymentRefundType.setModifiedId(tokenUser.getId());
        paymentRefundType.setModifiedName(tokenUser.getUsername());
        paymentRefundType.setGmtCreate(now);
        paymentRefundType.setGmtModified(now);
        paymentRefundType.setIsDeleted(IsDeletedCode.NO.getCode());
        baseMapper.insert(paymentRefundType);
        return Result.success(id);
    }

    @Override
    public Result update(MainDataPaymentRefundTypeReq req, TokenUser tokenUser) {
        LocalDateTime now = LocalDateTime.now();
        MainDataPaymentRefundType paymentRefundType = EntityUtil.copy(req, MainDataPaymentRefundType.class);
        paymentRefundType.setModifiedId(tokenUser.getId());
        paymentRefundType.setModifiedName(tokenUser.getUsername());
        paymentRefundType.setGmtModified(now);
        baseMapper.updateById(paymentRefundType);
        return Result.success(req.getId());
    }

    @Override
    public MainDataPaymentRefundTypeDTO query(MainDataPaymentRefundTypeIdReq req) {
        MainDataPaymentRefundType mainDataPaymentRefundType = baseMapper.selectById(req.getId());
        MainDataPaymentRefundTypeDTO paymentRefundTypeDTO = EntityUtil.copy(mainDataPaymentRefundType, MainDataPaymentRefundTypeDTO.class);
        return paymentRefundTypeDTO;
    }

    @Override
    public PageResponse<MainDataPaymentRefundTypeDTO> list(MainDataPaymentRefundTypeListReq req) {
        Page<MainDataPaymentRefundType> page = new Page<>(req.getPageNo(), req.getPageSize());
        QueryWrapper<MainDataPaymentRefundType> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(null != req.getStatus(),MainDataPaymentRefundType::getStatus,req.getStatus())
                .eq(StringUtils.isNotBlank(req.getDailyPayBusinessType()),MainDataPaymentRefundType::getDailyPayBusinessType,req.getDailyPayBusinessType())
                .eq(MainDataPaymentRefundType::getIsDeleted,IsDeletedCode.NO.getCode())
                .like(StringUtils.isNotBlank(req.getPaymentRefundName()),MainDataPaymentRefundType::getPaymentRefundName,req.getPaymentRefundName())
                .orderByDesc(MainDataPaymentRefundType::getGmtModified);
        Page<MainDataPaymentRefundType> iPage = baseMapper.selectPage(page, queryWrapper);
        List<MainDataPaymentRefundType> records = iPage.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            return new PageResponse<>(new ArrayList<>(), iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
        }
        List<MainDataPaymentRefundTypeDTO> mainDataPaymentRefundTypeDTOS = EntityUtil.copyList(records, MainDataPaymentRefundTypeDTO.class);
        return new PageResponse<>(mainDataPaymentRefundTypeDTOS, iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());

    }
}
